package com.hy.formula.function;

import com.hy.formula.common.CollectionUtils;
import com.hy.formula.runtime.FRuntimeContext;

/**
 * 减法
 * @author huangy
 * @date 2015年4月30日 下午2:35:35
 */
public class SubFunc extends AbsNumCalculateFunc implements IFunction {

	@Override
	public String getName() {
		return "sub";
	}

	@Override
	public Object execute(FRuntimeContext context, Object... params) {
		if(CollectionUtils.isEmpty(params) || params.length != 2){
			throw new IllegalArgumentException("参数个数不对");
		}
		Object o1 = params[0];
		Object o2 = params[1];
		if(hasDouble(o1,o2)){
			String s1 = params[0].toString();
			Double num1 = Double.valueOf(s1);
			
			String s2 = params[1].toString();
			Double num2 = Double.valueOf(s2);
			
			return num1 - num2;
		}else if(hasFloat(o1,o2)){
			String s1 = params[0].toString();
			float num1 = Float.valueOf(s1);
			
			String s2 = params[1].toString();
			float num2 = Float.valueOf(s2);
			
			return num1 - num2;
		}else{
			return (int)params[0] - (int)params[1];
		}
	}

	@Override
	public String description() {
		return "减法";
	}

}
